package kg.aibek.projecteuler.p10;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Problem26 {
	public void solve() {
		int number = 1;
		int d = 1;
		for (int i = 1; i < 1000; i++) {
			if (!isPrime(i))
				continue;
			try {
				BigDecimal.ONE.divide(new BigDecimal(i));
			} catch (ArithmeticException e) {
				String str = BigDecimal.ONE.divide(new BigDecimal(i), 10000,
						RoundingMode.HALF_UP).toString();

				String c = getRecurringCycleCount(str.substring(str
						.indexOf('.')));
				if (c.length() >= number) {
					number = c.length();
					d = i;
				}
			}
		}
		System.out.println(d);
	}

	private boolean isPrime(int number) {
		for (int i = 2; i * i < number; i++) {
			if (number % i == 0)
				return false;
		}
		return true;
	}

	private String getRecurringCycleCount(String number) {
		String regex = "([0-9]{7,}?)\\1+[0-9]*?$";
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher(number);
		if (matcher.find()) {
			return matcher.group(1);
		}
		return "";
	}

	public static void main(String[] args) {
		Problem26 p = new Problem26();
		p.solve();
	}
}
